package view.customer;

import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXDatePicker;
import com.jfoenix.controls.JFXTextField;
import controller.OrderController;
import dao.EquipMsgDao;
import dao.OrderDao;
import dao.ProductMsgDao;
import dao.UserDao;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.DatePicker;
import pojo.equip.EquipMsgEntity;
import pojo.order.OrderEntity;
import pojo.product.ProductMsgEntity;
import view.Main;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author Limohan
 */
public class ChangeOrderController {
    private Main myApp;
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    private static OrderDao orderDao = new OrderDao();
    private static UserDao userDao = new UserDao();
    private static String  id;
    private static String userId;
    public void setApp(Main myApp) {
        this.myApp = myApp;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
       this. userId = userId;
    }

    public ChangeOrderController() {
    }
    @FXML
    private JFXTextField amount;

    @FXML
    private JFXTextField cosignee;

    @FXML
    private JFXComboBox<String> productName;

    @FXML
    private JFXTextField address;

    @FXML
    private DatePicker handInTimePicker;

    @FXML
    private DatePicker deadLinePicker;

    @FXML
    private JFXTextField phone;

    @FXML
    void initialize()throws IOException {
        ProductMsgDao productMsgDao = new ProductMsgDao();
        for(ProductMsgEntity productMsgEntity:productMsgDao.findMsgList()){
            productName.getItems().add(productMsgEntity.getName());
        }
    }

    @FXML
    void returnManage(ActionEvent event) throws IOException {
        myApp.goToCustomerOrderPage();
    }

    @FXML
    void saveNewOrder(ActionEvent event) throws IOException {
        orderDao.deleteOrder(id);
        OrderEntity order = new OrderEntity();
        OrderController orderController = OrderController.getInstance();
        Date date = new Date();
        order.setCode("PD"+sdf.format(date));
        order.setName(productName.getValue());
        order.setAddress(address.getText());
        order.setAmount(amount.getText());
        order.setConsignee(cosignee.getText());
        order.setHandInDate(handInTimePicker.getValue().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        order.setDeahline(deadLinePicker.getValue().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        order.setPhone(phone.getText());
        order.setOrderStateForCustomer("已保存");
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("!");
        alert.setContentText(orderController.addMsg(order));
        alert.showAndWait();

    }

    public void originDisplay() throws IOException{
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        OrderEntity orderEntity = orderDao.findOrder(id);
        productName.setValue(orderEntity.getName());
        address.setText(orderEntity.getAddress());
        amount.setText(orderEntity.getAmount());
        cosignee.setText(orderEntity.getConsignee());
        handInTimePicker.setValue(LocalDate.parse(orderEntity.getHandInDate(),fmt));
        deadLinePicker.setValue(LocalDate.parse(orderEntity.getDeahline(),fmt));
        phone.setText(orderEntity.getPhone());
    }
}
